package classe.grafica;                                                        

import classes.gerais.*;                                                        //Importa os elementos das classes gerais   
import static java.lang.System.exit;                                            //Importa para o exit(1)
import javax.swing.JOptionPane;                                                 //Importa para mostrar as mensagens

public class Login extends javax.swing.JFrame {                                 //Classe Login - Extende JFrame
   
    private Usuario User;                                                       //Usuário que fará o login
    
    public Login() {                                                            //Montador que inicializa um usuário inexistente
        initComponents();                                                       //Inicia os cmponentes
        this.User = null;                                                       //Anula o usuário
    }
    
    public Login (Usuario UserR){                                               //Montador que inicia um usuário existente
        initComponents();                                                       //Inicia os componentes
        this.User = UserR;                                                      //Refrencia o usuário
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BLEntrar = new javax.swing.JButton();
        BLSair = new javax.swing.JButton();
        PfLSenha = new javax.swing.JPasswordField();
        TfLLogin = new javax.swing.JTextField();
        LLLogin = new javax.swing.JLabel();
        LLSenha = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CER - Login");

        BLEntrar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BLEntrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/classe/grafica/lock.png"))); // NOI18N
        BLEntrar.setText("Entrar");
        BLEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BLEntrarActionPerformed(evt);
            }
        });

        BLSair.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BLSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/classe/grafica/back_alt.png"))); // NOI18N
        BLSair.setText("Sair");
        BLSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BLSairActionPerformed(evt);
            }
        });

        LLLogin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        LLLogin.setText("Login:");

        LLSenha.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        LLSenha.setText("Senha:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(LLLogin, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(LLSenha, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(PfLSenha)
                            .addComponent(TfLLogin)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(BLEntrar, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BLSair, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TfLLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LLLogin))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PfLSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LLSenha))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BLSair)
                    .addComponent(BLEntrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BLSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BLSairActionPerformed
        exit(1);                                                                //Termina o programa
    }//GEN-LAST:event_BLSairActionPerformed

    private void BLEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BLEntrarActionPerformed
        if (TfLLogin.getText().equals(User.DevolveNDU())){                          //Se o usuário for correto
            if (PfLSenha.getText().equals(User.DevolveSenha())){                    //Se a senha for a correta  
                System.out.printf("Seja bem-vindo(a) %s\n", User.DevolveNome());    //O usuário entrou
                this.setVisible(false);                                             //Deixa a tela de login invisível
                Principal TelaP = new Principal ();                                 //Cria uma tela principal
                TelaP.setLocationRelativeTo(null);                                  //Centraliza a tela
                TelaP.show();                                                       //Mostra a tela
            }
            else JOptionPane.showMessageDialog(null,"Senha incorreta!!");           //Se a senha estiver incorreta
        }
        else JOptionPane.showMessageDialog(null,"Usuário incorreto!!");             //Se o usuário esta incorreto
    }//GEN-LAST:event_BLEntrarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BLEntrar;
    private javax.swing.JButton BLSair;
    private javax.swing.JLabel LLLogin;
    private javax.swing.JLabel LLSenha;
    private javax.swing.JPasswordField PfLSenha;
    private javax.swing.JTextField TfLLogin;
    // End of variables declaration//GEN-END:variables
}
